<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ListArticlesPage
 *
 * @author julien
 */
require_once 'Classes/View/DefaultHTMLPage.php';
require_once 'Classes/View/AbstractedViewItems/CellItem.php';
require_once 'Classes/View/AbstractedViewItems/AdminActionsLinks.php';

require_once 'Classes/View/ViewItems/ListItem.php';

class ListBasedPage extends DefaultHTMLPage{
    private $elements;
    public function __construct($title) {
        parent::__construct($title);
        $this->elements = array();
        
    }
    
    public function addElement($element){
        if(is_array($element)){
            foreach ($element as $key => $value) {
                $this->addElement($value);
            }
        }
        else if (is_null($element)){
            $message = new TextFieldItem('There\'s no element in this category!');
            $this->addContent($message);
        }
        else if(is_a($element,"Element")){
            $lastElement = count($this->elements);
            $this->elements[$lastElement] = new CellItem();
            if(method_exists($element, "getName")){

                if(preg_match('/^.*\/$/', $_SERVER['REQUEST_URI'])){
                    $title = new LinkItem($_SERVER['REQUEST_URI'].$element->getId(),$element->getName());
                }
                else {
                    $title = new LinkItem($_SERVER['REQUEST_URI'].'/edit-'.$element->getId(),$element->getName());
                }
               // $title->setClass("cellTitle");
                $this->elements[$lastElement]->setTitle($title->getHTML());
                $this->elements[$lastElement]->unsetEndingSpace();
                $this->elements[$lastElement]->addContent(new AdminActionsLinks($element->getId()));
               
            }
            
            if(method_exists($element, "getDescription")){
                $this->elements[$lastElement]->addContent($element->getDescription());
                $this->elements[$lastElement]->setClass("Description");
            }
            
            if(method_exists($element, "getOptionalFields")){
                $listOpt = new ListItem();
                $listOpt->setClass("OptList");
                foreach ($element->getOptionalFields() as $key => $value) {
                    $listOpt->addElement($key." : ".$value);
                }       
                $this->elements[$lastElement]->addContent($listOpt);
            }
            
            
            
            if(method_exists($element,"getAddress")){
                $this->elements[$lastElement]->addContent($element->getAddress());
            }
            
            $this->addContent($this->elements[$lastElement]);
        }
    }
}
